package com.iflytek.ci.common;

import com.iflytek.common.security.exception.MissUserSessionException;
import com.iflytek.iframework.orm.hibernate.HibernateEntityDao;
import com.iflytek.iframework.utils.StringUtils;
import com.iflytek.jsec.cac.domain.Authority;
import com.iflytek.jsec.cac.domain.User;
import com.iflytek.jsec.cac.domain.UserInfo;
import com.iflytek.jsec.cac.domain.UserRole;
import com.iflytek.jsec.cac.service.security.JsecService;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 产品化 SecurityManager
 * 
 * @author quansun
 * @version $Id: CPHSecurityManager.java 48736 2011-12-20 08:06:31Z hyzha $s
 */
@SuppressWarnings("unchecked")
public class CPHSecurityManager extends HibernateEntityDao implements
        BusinessSecurityManager {
    
    /**
     * jsecService
     */
    @Autowired
    private JsecService jsecService;

    /**
     * 
     *  @see com.iflytek.common.security.BusinessSecurityManager#getCurrentUser(javax.servlet.http.HttpSession)
     *  @author scrum
     *  @created 2013-6-23 下午04:05:34
     *  @lastModified      
     *  @history
     */
    public User getCurrentUser(HttpSession session) {
        return getCurrentUser();
    }

    /**
     * 
     *  @see com.iflytek.common.security.BusinessSecurityManager#getCurrentUser()
     *  @author scrum
     *  @created 2013-6-23 下午04:05:56
     *  @lastModified      
     *  @history
     */
    public User getCurrentUser() {
        User user = jsecService.getCurrentUser();
        if (null == user) {
            throw new MissUserSessionException();
        }
        return user;
    }

    /**
     * 
     *  @see com.iflytek.common.security.BusinessSecurityManager#getUsers(String)
     *  @author scrum
     *  @created 2013-6-23 下午04:05:47
     *  @lastModified
     *  @history
     */
    public List<User> getUsers(String orgCode) {
        // List userList = this.find(
        // " from User user where user.organization.id='" + orgCode + "'");
        List userList = jsecService.getUserListByOrgId(orgCode);
        return userList;
    }

    /**
     *
     *  @see com.iflytek.common.security.BusinessSecurityManager#getUserByLoginName(String)
     *  @author scrum
     *  @created 2013-6-23 下午04:06:06
     *  @lastModified
     *  @history
     */
    public User getUserByLoginName(String userLoginName) {
        User user = jsecService.getUserByLoginName(userLoginName);
        if (null == user) {
            throw new MissUserSessionException();
        }
        return user;
    }

    /**
     *
     *  @see com.iflytek.common.security.BusinessSecurityManager#getRoleMcsOfUser(com.iflytek.jsec.cac.domain.User)
     *  @author scrum
     *  @created 2013-6-23 下午04:06:15
     *  @lastModified
     *  @history
     */
    public String getRoleMcsOfUser(User user) {
        StringBuilder roleMcs = new StringBuilder();
        List<UserRole> roles = user.getUserRoleList();
        if (roles != null) {
            for (UserRole role : roles) {
                roleMcs.append(role.getRole().getId());
            }
        }
        return roleMcs.toString();
    }

    /**
     *
     *  @see com.iflytek.common.security.BusinessSecurityManager#getFirstMenhuOfCurrentUser()
     *  @author scrum
     *  @created 2013-6-23 下午04:06:23
     *  @lastModified
     *  @history
     */
    public List<Authority> getFirstMenhuOfCurrentUser() {
        List<Authority> list = jsecService.getFirstAuthorityListByCurrentUser();
        if (null == list) {
            return list;
        }
        List<Authority> data = new ArrayList<Authority>(10);
        for (int i = list.size() - 1; i >= 0; i--) {
            data.add(list.get(i));
        }
        return data;
    }

    /**
     *
     *  @see com.iflytek.common.security.BusinessSecurityManager#getFrameSetWebContext()
     *  @author scrum
     *  @created 2013-6-23 下午04:06:48
     *  @lastModified
     *  @history
     */
    public String getFrameSetWebContext() {
        return jsecService.getFrameSetWebContext();
    }

    /**
     *
     *  @see com.iflytek.common.security.BusinessSecurityManager#getCurrentUserInfo(javax.servlet.http.HttpSession)
     *  @author scrum
     *  @created 2013-6-23 下午04:06:52
     *  @lastModified
     *  @history
     */
    public UserInfo getCurrentUserInfo(HttpSession session) {
        return jsecService.getCurrentUserInfo();
    }

    /**
     *
     *  @see com.iflytek.common.security.BusinessSecurityManager#getUserInfoByLoginName(String)
     *  @author scrum
     *  @created 2013-6-23 下午04:06:58
     *  @lastModified
     *  @history
     */
    public UserInfo getUserInfoByLoginName(String userLoginName) {
        return jsecService.getUserInfoByLoginName(userLoginName);
    }

    /**
     *
     *  @see com.iflytek.common.security.BusinessSecurityManager#getUserBySfzh(String)
     *  @author scrum
     *  @created 2013-6-23 下午04:07:03
     *  @lastModified
     *  @history
     */
    public User getUserBySfzh(String sfzh) {
        String sql = "Select b.Login_Name "
                + " From t_Jsec_User_Info a, t_Jsec_User b "
                + " Where a.User_Id = b.Id " + " And a.Sfzh =?";

        List data = super.createSqlQuery(sql, sfzh).setResultTransformer(
                Transformers.ALIAS_TO_ENTITY_MAP).list();

        if (data == null || data.isEmpty())
            return null;

        Map map = (Map) data.get(0);
        User user = new User();
        user.setLoginName((String) map.get("LOGIN_NAME"));
        return user;
    }

    /**
     *
     *  @see com.iflytek.common.security.BusinessSecurityManager#isUserHasAuth(com.iflytek.jsec.cac.domain.User, String)
     *  @author scrum
     *  @created 2013-6-23 下午04:07:08
     *  @lastModified
     *  @history
     */
    public boolean isUserHasAuth(User user, String authName) {
        boolean flag = false;
        List<Authority> auths = jsecService.getAuthorityListByUserId(user
                .getId());
        for (Authority authority : auths) {
            if (authName.equalsIgnoreCase(authority.getName())) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     *
     *  @see com.iflytek.common.security.BusinessSecurityManager#isUserHasRole(com.iflytek.jsec.cac.domain.User, String)
     *  @author scrum
     *  @created 2013-6-23 下午04:07:12
     *  @lastModified
     *  @history
     */
    public boolean isUserHasRole(User user, String roleId) {
        List<UserRole> roles = user.getUserRoleList();
        if (StringUtils.isNotBlank(roleId) && roles != null) {
            for (UserRole role : roles) {
                if (roleId.equals(role.getRole().getId())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * @Title: getMenuByMenuName
     * @Description: 见接口说明
     * @param menuName
     * @return
     * @see com.iflytek.common.security.BusinessSecurityManager#getMenuByMenuName(String)
     * @throws
     */
    public String getMenuByMenuName(String menuName) {
        String sql = "select web_context from t_jsec_subsystem t where t.sub_systen_name=?";
        Object obj = super.createSqlQuery(sql, menuName).uniqueResult();
        if (obj != null)
            return obj.toString();
        return "";
    }

}
